home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 25 / CU Amiga Magazine's Super CD-ROM 25 (1998)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1998-08].iso / CUCD / Magazine / C_Tutorial / Part-11 / req0 / loadsave.c < prev    next >
C/C++ Source or Header  |  1998-03-02  |  4KB  |  168 lines

  1. #include "loadsave.h"
  2. #include "bitmap.h"
  3. #include "drawwin.h"
  4. #include "gui.h"
  5. #include "main.h"
  6.  
  7. #include "iff.h"
  8.  
  9. #include<libraries/asl.h>
  10. #include<intuition/intuition.h>
  11. #include<workbench/workbench.h>
  12.  
  13. #include<string.h>
  14. #include<stdio.h>
  15.  
  16. #include<clib/asl_protos.h>
  17. #include<clib/dos_protos.h>
  18. #include<clib/graphics_protos.h>
  19. #include<clib/icon_protos.h>
  20.  
  21. /* Global handles for our requesters */
  22. static struct FileRequester* loadreq = NULL;
  23. static struct FileRequester* savereq = NULL;
  24.  
  25. /* Open an ASL load file requester */
  26. int load()
  27. {
  28.     /* Allocate the requester if we haven't already */
  29.     if(loadreq == NULL)
  30.         loadreq = (struct FileRequester*)
  31.             AllocAslRequestTags(ASL_FileRequest,
  32.                                                     ASLFR_TitleText,            "Load File",
  33.                                                     ASLFR_Flags1,                    FRF_DOPATTERNS,
  34.                                                     ASLFR_InitialPattern,    "#?.iff",
  35.                                                     TAG_DONE);
  36.     if(loadreq)
  37.     {
  38.         struct Window* win = getDrawWin();
  39.         if(AslRequestTags(loadreq, ASLFR_Window, win, TAG_DONE))
  40.         {
  41.             char filename[MAXFILENAME];
  42.             /* Create complete filename from ASL's dir and file */
  43.             strcpy(filename, loadreq->rf_Dir);
  44.             if(AddPart(filename, loadreq->rf_File, MAXFILENAME))
  45.                 return loadfile(filename);
  46.             else
  47.                 printf("Error: could not make filename\n");
  48.         }
  49.         /* else: requester was cancelled */
  50.     }
  51.     else
  52.         printf("Error: could not allocate ASL (load) file request\n");
  53.     /* If we get this far then there was no fatal error */
  54.     return TRUE;
  55. }
  56.  
  57. int loadfile(char* filename)
  58. {
  59.     /* Record any fatal errors */
  60.     int going = TRUE;
  61.     IFFL_HANDLE handle;
  62.     /* Try to open the IFF file */
  63.     if(handle = IFFL_OpenIFF(filename, IFFL_MODE_READ))
  64.     {
  65.         UWORD colortable[256];
  66.         /* Get colour information */
  67.         LONG count = IFFL_GetColorTab(handle, colortable);
  68.         /* Get display information */
  69.         ULONG displayid = IFFL_GetViewModes(handle);
  70.         /* Get picture information */
  71.         struct IFFL_BMHD* bmhd = IFFL_GetBMHD(handle);
  72.         struct Window* win;
  73.         /* Try to adjust the screen to fit */
  74.         if(bmhd)
  75.         {
  76.             closeGUI();
  77.             /* If this fails, our local win will be NULL */
  78.             openGUI(bmhd->nPlanes, bmhd->w, bmhd->h, displayid);
  79.         }
  80.         if(win = getDrawWin())
  81.         {
  82.             /* Change screen colours */
  83.             LoadRGB4(&(win->WScreen->ViewPort), colortable, count);
  84.             /* If we can load the picture, update window's display */
  85.             if(IFFL_DecodePic(handle, getBitmap()))
  86.                 CopySBitMap(win->WLayer);
  87.             else
  88.                 printf("Error: could not decode IFF picture\n");
  89.         }
  90.         else
  91.             going = FALSE;  /* The only fatal error */
  92.         IFFL_CloseIFF(handle);
  93.     }
  94.     else
  95.         printf("Error: could not open IFF file\n");
  96.     return going;
  97. }
  98.  
  99. /* Open an ASL save file requester */
  100. void save()
  101. {
  102.     /* Another way of saying "allocate if we haven't already" */
  103.     if(savereq ||
  104.         (savereq = (struct FileRequester*)
  105.             AllocAslRequestTags(ASL_FileRequest,
  106.                                                     ASLFR_TitleText,            "Save File",
  107.                                                     ASLFR_Flags1,                    FRF_DOPATTERNS | FRF_DOSAVEMODE,
  108.                                                     ASLFR_InitialPattern,    "#?.iff",
  109.                                                     ASLFR_InitialFile,        "picture.iff",
  110.                                                     TAG_DONE)))
  111.     {
  112.         struct Window* win = getDrawWin();
  113.         if(AslRequestTags(savereq, ASLFR_Window, win, TAG_DONE))
  114.         {
  115.             char filename[MAXFILENAME];
  116.             /* Create complete filename from ASL's dir and file */
  117.             strcpy(filename, savereq->rf_Dir);
  118.             if(AddPart(filename, savereq->rf_File, MAXFILENAME))
  119.             {
  120.                 /* Make sure our bitmap is the same as the display */
  121.                 SyncSBitMap(win->WLayer);
  122.                 /* Try saving our bitmap, using the screen's colours */
  123.                 if(IFFL_SaveBitMap(filename, getBitmap(),
  124.                                                     win->WScreen->ViewPort.ColorMap->ColorTable,
  125.                                                     IFFL_COMPR_BYTERUN1))
  126.                 {
  127.                     /* Write an icon for the file */
  128.                     struct DiskObject* dobj = GetDefDiskObject(WBPROJECT);
  129.                     if(dobj)
  130.                     {
  131.                         /* Temporarily change the default tool */
  132.                         char* dtool = dobj->do_DefaultTool;
  133.                         dobj->do_DefaultTool = progName();
  134.                         /* Write out our icon */
  135.                         PutDiskObject(filename, dobj);
  136.                         /* Reinstate the default tool */
  137.                         dobj->do_DefaultTool = dtool;
  138.                         FreeDiskObject(dobj);
  139.                     }
  140.                 }
  141.                 else
  142.                     printf("Error: could not write IFF picture\n");
  143.             }
  144.             else
  145.                 printf("Error: could not make filename\n");
  146.         }
  147.         /* else: requester was cancelled */
  148.     }
  149.     else
  150.         printf("Error: could not allocate ASL (save) file request\n");
  151. }
  152.  
  153. /* Free any requesters that may have been allocated */
  154. void freeReqs()
  155. {
  156.     if(loadreq)
  157.     {
  158.         FreeAslRequest(loadreq);
  159.         loadreq = NULL;
  160.     }
  161.     if(savereq)
  162.     {
  163.         FreeAslRequest(savereq);
  164.         savereq = NULL;
  165.     }
  166. }
  167.  
  168.